package priv.yiyuan.example.design;

import java.util.ArrayList;
import java.util.List;

/**
 * 复合图形类
 * <p>
 * 组合模式(组件)
 */
public class CompositeShape extends Shape {

    private List<Shape> children = new ArrayList<>();

    private String compositeName;

    public CompositeShape(Renderer renderer, String compositeName) {
        super(renderer);
        this.compositeName = compositeName;
    }

    @Override
    public void draw() {
        System.out.println("---开始绘制名为 [" + compositeName + "] 的复合图---");
        for (Shape shape : children) {
            shape.setRenderer(shape.renderer);
            shape.draw();
        }
        System.out.println("---名为 [" + compositeName + "] 的复合图绘制结束---");
    }

    @Override
    public void setRenderer(Renderer renderer) {
        super.setRenderer(renderer);
        for (Shape child : children) {
            child.setRenderer(this.renderer);
        }
    }

    @Override
    public void add(Shape shape) {
        children.add(shape);
    }

    @Override
    public void remove(Shape shape) {
        children.remove(shape);
    }

    @Override
    public void getChild(int i) {
        children.get(i);
    }

}
